/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.nist.ssd.swqb.core.files;

import com.hp.hpl.jena.rdf.model.Model;
import gov.nist.ssd.swqb.core.ConfigFileManager;
import gov.nist.ssd.swqb.core.exceptions.OutOfBoundsException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author benjamin herbomez
 */
public class PrefixManager extends PairFileManager {

    /**
     * Construct prefixManager from file
     * The file contain user predefined prefixs
     * @param fm 
     */
    public PrefixManager(ConfigFileManager fm) {
        super(fm);
        this.fileName = "prefixs";
        this.init();
    }

    /**
     * Add non existants prefixs to the list
     * @param model
     * @return new entries
     */
    public Map<String, String> addFromModel(final Model model) {
        Map<String, String> r = new LinkedHashMap<String, String>();
        Map<String, String> nsPrefixMap = model.getNsPrefixMap();
        for(String pref : nsPrefixMap.keySet()){
            if(!pairs.containsValue(nsPrefixMap.get(pref)))
                r.put(nsPrefixMap.get(pref), pref);
        }
        return r;
    }

    public Set<String> getNamespaces() {
        return this.pairs.keySet();
    }

    public Collection<String> getPrefixs() {
        return this.pairs.values();
    }

    public Map<String, String> getTable() {
        return this.pairs;
    }

    public String getPrefix(String ns) throws OutOfBoundsException {
        try {
            return this.pairs.get(ns);
        } catch (Exception e) {
            throw new OutOfBoundsException();
        }
    }

    public void setPrefix(String ns, String prefix) {
        this.pairs.put(ns, prefix);
    }

    public String getPrefixsTurtleFormat() {
        String r = "";
        for (String ns : this.pairs.keySet()) {
            r += "PREFIX " + this.pairs.get(ns) + ": "
                    + "<" + ns + ">\n";
        }
        return r;
    }

    public void remove(String valueAt) {
        if(this.pairs.containsKey(valueAt))
            this.pairs.remove(valueAt);
        save();
    }
}
